class MinHeap<T> {
    protected heap:T[];
    constructor(){
        this.heap=[];
    }
    // 获取左子树位置
    protected getLeftIndex(index:number):number{
        return 2 * index + 1;
    }
    protected getRightIndex(index:number):number{
        return 2*index+2;
    }
    protected getParentIndex(index:number):number|undefined{
        if(index==0){
            return undefined
        }
        return Math.floor((index-1)/2)
    }
    // 实现插入函数
    insert(value:T):boolean{
        if(value!=null){
            this.heap.push(value);
            // 维护堆  上浮 递归
            this.shiftup(this.heap.length-1)
        }
        return false;
    }
    protected shiftup(index:number):void{
        let parent = <number>this.getParentIndex(index)
        while(index > 0 && this.heap[parent] > this.heap[index]){
            this.swap(this.heap,parent,index)
            index = parent;
            parent =<number> this.getParentIndex(index);
        }
    }
    protected swap (array:T[],exchangeElement:number,exchangedElement:number){
        const temp = array[exchangeElement];
        array[exchangeElement]=array[exchangedElement];
        array[exchangedElement]=temp;
    }
    toString(){
        return this.heap.join('')
    }
}
class MaxHeap<T> extends MinHeap<T>{

}
const array = [33,17,21,16,13,15,9,5,6,7,8,1,2]

const heap=new MinHeap()

for(let i =0;i<array.length;i++){
    heap.insert(array[i])
}

console.log(heap.toString())